﻿using MediatR;
using PublicClass;
using S.Eartag.API.Application.Command.SheepFileConditionCommand;
using S.Eartag.Domain;
using S.Eartag.Infrastructure.Interface;

namespace S.Eartag.API.Application.CommandHandlers.SheepFileConditionCommandHandler
{
    public class SheepFileConditionUpdateprogenyCommandHandler : IRequestHandler<SheepFileConditionUpdateprogenyCommand, int>
    {
        private readonly ISheepFileRepository sheepFileRepository;

        public SheepFileConditionUpdateprogenyCommandHandler(ISheepFileRepository sheepFileRepository)
        {
            this.sheepFileRepository = sheepFileRepository;
        }

        public async Task<int> Handle(SheepFileConditionUpdateprogenyCommand request, CancellationToken cancellationToken)
        {
            var res = sheepFileRepository.GetValues(x => x.EarNumber == request.card).Result.FirstOrDefault();
            if (res.sheepType== SheepType.种公 || res.sheepType == SheepType.种母)
            {
                res.sheepType=SheepType.后裔;
            }
            else
            {
                if (res.sex==sex.公)
                {
                    res.sheepType = SheepType.种公;
                }
                else
                {
                    res.sheepType = SheepType.种母;
                }
            }
            return await sheepFileRepository.Update(res);
        }
    }
}
